package com.jkj.http.sys.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkj.http.controller.BaseController;
import com.jkj.http.entity.LayPadding;
import com.jkj.http.interceptor.AuthorizeChecked;
import com.jkj.http.sys.entity.SysItemsDetail;
import com.jkj.http.sys.entity.SysLog;
import com.jkj.http.sys.service.SysItemDetailService;
import com.jkj.utils.JsonUtils;
import com.jkj.utils.OperatorProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping(value = "/system/itemsDetail")
public class ItemDetailController extends BaseController {
    @Autowired
    private SysItemDetailService itemDetailService;

    /**
     * 字典明细列表画面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public ModelAndView index() {
        return new ModelAndView("system/itemsDetail/index");
    }

    /**
     * 字典明细画面查询（分页）
     *
     * @param pageIndex
     * @param pageSize
     * @param itemId
     * @param keyWord
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String index(int pageIndex, int pageSize, String itemId, String keyWord) {
        int totalCount = itemDetailService.getTotalCount(itemId, keyWord);
        PageHelper.startPage(pageIndex, pageSize);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<SysItemsDetail> list = itemDetailService.getList(itemId, keyWord);
        PageInfo<SysItemsDetail> pageInfo = new PageInfo<>(list);

        List<SysItemsDetail> pageData = pageInfo.getList();
        LayPadding<SysItemsDetail> result = new LayPadding<>();
        result.setResult(true);
        result.setMsg("success");
        result.setList(pageData);
        result.setCount(totalCount);
        return JsonUtils.toJSON(result);
    }

    /**
     * 字典明细新增或者修改画面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/form", method = {RequestMethod.GET})
    public ModelAndView form() {
        return new ModelAndView("system/itemsDetail/form");
    }


    /**
     * 字典明细新增或者修改
     *
     * @param session
     * @param model
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/form", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String form(HttpSession session, SysItemsDetail model) {
        if (StrUtil.isEmpty(model.getId())) {
            model.setCreateUser(OperatorProvider.getCurrent(session).getAccount());
            int row = itemDetailService.insert(model);
            return row > 0 ? success() : error();
        } else {
            model.setModifyUser(OperatorProvider.getCurrent(session).getAccount());
            int row = itemDetailService.update(model);
            return row > 0 ? success() : error();
        }
    }

    /**
     * 字典明细详情画面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/detail", method = {RequestMethod.GET})
    public ModelAndView detail() {
        return new ModelAndView("system/itemsDetail/detail");
    }

    /**
     * 删除字典明细
     *
     * @param primaryKey
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/delete", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String delete(String primaryKey) {
        int row = itemDetailService.delete(primaryKey);
        return row > 0 ? success() : error();
    }

    /**
     * 获取字典明细信息
     *
     * @param primaryKey
     * @return
     */
    @RequestMapping(value = "/getForm", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String getForm(String primaryKey) {
        SysItemsDetail entity = itemDetailService.get(primaryKey);
        entity.setIsDefault(entity.getIsDefault().equals("1") ? "true" : "false");
        entity.setIsEnabled(entity.getIsEnabled().equals("1") ? "true" : "false");
        return JsonUtils.toJSON(entity);
    }
}